package com.briup.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.domain.User;
import com.briup.domain.extend.UserExtend;
import com.briup.resuit.Result;
import com.briup.service.UserService;
import com.briup.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    UserService userService;
    @ApiOperation("新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        user.setPassword(MD5Util.MD5(user.getPassword()));
        userService.saveUser(user);
        return Result.success("新增成功");
    }
    @ApiOperation("根据id查找用户")
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable Long id) {
        User byId = userService.getById(id);
        return Result.success(byId);
    }
    @ApiOperation("设置用户为Vip")
    @PutMapping("/setVip/{id}")
    public Result setVip(@PathVariable Long id) {
        userService.setVip(id);
        return Result.success("更新成功");
    }
    @ApiOperation("根据id删除用户")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteByBatch(@PathVariable List<Long> ids){
        userService.removeByIds(ids);
        return Result.success("删除成功");
    }
    @ApiOperation("更新用户信息")
    @PutMapping("/update")
    public Result updateById(@RequestBody User user) {
        userService.updateById(user);
        return Result.success("更新成功");
    }
    @ApiOperation( "分页+条件查询用户")
            @GetMapping("/query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码",
                    required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条 数", required = true, dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "username", value = "用户名",
                    dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：启用| 禁用", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "roleId", value = "角色id",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isVip", value = "是否为会员: 0|1", dataType = "int", paramType = "query")
                    })
    public Result query(@RequestParam Integer pageNum, @RequestParam Integer pageSize, String
            username, String status, Integer roleId, Integer isVip){
        IPage<UserExtend> userExtends= userService.queryAll(pageNum, pageSize, username, status, roleId, isVip);
        return Result.success(userExtends);
    }
    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")
    public Result info(@RequestAttribute("userId") Long id){
        log.info("id:{}",id);
        return Result.success(userService.getById(id));
    }
}
